package My.project.Util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import My.project.Entity.People;
import My.project.Entity.Video;

/**
 * Responsible for parsing xml strings returned from server.
 * @author Bell
 *
 */
public class XmlParser {
	DocumentBuilderFactory domfac = null;
	DocumentBuilder dombuilder = null;
	
	public XmlParser() {
		domfac = DocumentBuilderFactory.newInstance();
		try {
			dombuilder = domfac.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Parse result of get people from server.
	 * @param str The return result.
	 * @return List<People> List of People entity.
	 */
	public List<People> parsePeople(String str) {
		List<People> peopleList = new ArrayList<People>();
		InputStream is = new ByteArrayInputStream(str.getBytes());
		Document doc;
		try {
			doc = dombuilder.parse(is);
			Element root = doc.getDocumentElement();
			NodeList peopleRoot = root.getChildNodes();
			if(peopleRoot != null) {
				for(int i = 0; i < peopleRoot.getLength(); i++) {
					Node people = peopleRoot.item(i);
					if(people.getNodeType() == Node.ELEMENT_NODE) {
						peopleList.add(parseUserTag(people));
					}
				}
			}
			return peopleList;
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Parse result of get video from server.
	 * @param str The return result.
	 * @return List<Video> List of Video entity.
	 */
	public List<Video> parseVideo(String str) {
		List<Video> videoList = new ArrayList<Video>();
		InputStream is = new ByteArrayInputStream(str.getBytes());
		Document doc;
		try {
			doc = dombuilder.parse(is);
			Element root = doc.getDocumentElement();
			NodeList videos = root.getChildNodes();
			if(videos != null) {
				for(int i = 0; i < videos.getLength(); i++) {
					Node video = videos.item(i);
					if(video.getNodeType() == Node.ELEMENT_NODE) {
						videoList.add(parseVideoTag(video));
					}
				}
			}
			return videoList;
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// parse user tag
	private People parseUserTag(Node people) {
		String uid = null;
		String nick = null;
		String des = null;
		boolean isFriend = true;
		for(Node node = people.getFirstChild(); node != null;
				node = node.getNextSibling()) {
			if(node.getNodeName().equals("uid")) {
				uid = node.getTextContent();
			} else if (node.getNodeName().equals("nick")) {
				nick = node.getTextContent();
			} else if (node.getNodeName().equals("des")) {
				des = node.getTextContent();
			} else if (node.getNodeName().equals("ifFriend")) {
				String tmp = node.getTextContent();
				if (tmp.equals("false")) {
					isFriend = false;
				}
			}
		}
		return new People(uid, nick, des, isFriend);
	}
	
	// parse video tag
	private Video parseVideoTag(Node video) {
		String vname = null;
		String vdes = null;
		String vurl = null;
		for(Node node = video.getFirstChild(); node != null;
				node = node.getNextSibling()) {
			if(node.getNodeType() == Node.ELEMENT_NODE) {
				if(node.getNodeName().equals("VideoName")) {
					vname = node.getTextContent();
				} else if (node.getNodeName().equals("VideoDes")) {
					vdes = node.getTextContent();
				} else if (node.getNodeName().equals("rtmp")) {
					vurl = node.getTextContent();
				}
			}
		}
		return new Video(vname, vurl, vdes);
	}
}